<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Request;

class Integraldetailed extends Api
{
    public function index()
    {
        if($this->request->isPost())
        {
            $post = input('post.');
            $post = array_filter($post);
            $limit = $post['limit'] ?? 10;
            if(!isset($post['user_id'])){
                return fh(404,'error','缺少用户id',[]);
            }
            $model = model('integral')->where([
                ['user_id','=',$post['user_id']]
            ])->with('integraldetailed,order')->paginate($limit);
            return fh(200,'success','请求成功',$model);
        }
    }
    public function user()
    {
        if($this->request->isPost()){
            $post = input('post.');
            $post = array_filter($post);
            if(!isset($post['user_id'])){
                return fh(-1,'error','缺少用户id',[]);
            }
            $list = model($this->request->controller())->where('user_id','=',$post['user_id'])->select()->toArray();
            $money = ['cpdh'=>0,'ggzj'=>0,'dked'=>0,'sto'=>0,'sbwh'=>0,'ssjf'=>0];
            foreach ($list as $k=>$v){
                if($v['status'] == 1){
                    if($v['class'] == 1){
                        $money['cpdh'] = $money['cpdh']*1 + $v['num']*1;
                    }else{
                        $money['cpdh'] = $money['cpdh']*1 - $v['num']*1;
                    }
                }elseif($v['status'] == 2){
                    if($v['class'] == 1){
                        $money['ggzj'] = $money['ggzj']*1 + $v['num']*1;
                    }else{
                        $money['ggzj'] = $money['ggzj']*1 - $v['num']*1;
                    }
                }elseif($v['status'] == 3){
                    if($v['class'] == 1){
                        $money['dked'] = $money['dked']*1 + $v['num']*1;
                    }else{
                        $money['dked'] = $money['dked']*1 - $v['num']*1;
                    }
                }elseif($v['status'] == 4){
                    if($v['class'] == 1){
                        $money['sto'] = $money['sto']*1 + $v['num']*1;
                    }else{
                        $money['sto'] = $money['sto']*1 - $v['num']*1;
                    }
                }elseif($v['status'] == 5){
                    if($v['class'] == 1){
                        $money['sbwh'] = $money['sbwh']*1 + $v['num']*1;
                    }else{
                        $money['sbwh'] = $money['sbwh']*1 - $v['num']*1;
                    }
                }elseif($v['status'] == 6){
                    if($v['class'] == 1){
                        $money['ssjf'] = $money['ssjf']*1 + $v['num']*1;
                    }else{
                        $money['ssjf'] = $money['ssjf']*1 - $v['num']*1;
                    }
                }
            }
            return fh(200,'success','请求成功',$money);
        }else{
            return fh(404,'error','不是post请求',[]);
        }
    }
}